﻿Imports System.Windows.Forms
Imports System.ComponentModel

Public Class AccountToAccountUpdate
    Private Sub InputUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim 开始时间 As Date = New Date(Now.Year, Now.Month, 1)
        ''开始时间 = 开始时间.AddMonths(-1)
        'Dim 结束时间 As Date = New Date(Now.Year, Now.Month, Date.DaysInMonth(Now.Year, Now.Month))

        'Me.dt开始时间.Value = 开始时间
        'Me.dt结束时间.Value = 结束时间

        Button1_Click(Nothing, Nothing)
        If Me.rad全部折叠.Checked Then
            Me.OutlookGrid1.CollapseAll()
        Else
            Me.OutlookGrid1.ExpandAll()
        End If
    End Sub

#Region "查询上一个月按钮"
    Public Sub LoadData1(ByVal itype As Integer)

        Dim strBegin As String = Me.dt开始时间.Value.Year.ToString("0000") + "-" + Me.dt开始时间.Value.Month.ToString("00") + "-" + Me.dt开始时间.Value.Day.ToString("00")
        Dim strEnd As String = Me.dt结束时间.Value.Year.ToString("0000") + "-" + Me.dt结束时间.Value.Month.ToString("00") + "-" + Me.dt结束时间.Value.Day.ToString("00")

        Dim sb As New System.Text.StringBuilder
        sb.AppendLine(" SELECT CrashInOut.*  ")
        sb.AppendLine(" ,CrashStatus1.Title as Name1  ")
        sb.AppendLine(" ,CrashStatus2.Title as Name2  ")
        sb.AppendLine(" ,WysInputUser.username username1")
        sb.AppendLine(" FROM CrashInOut ")
        sb.AppendLine(" left join CrashStatus CrashStatus1 on CrashStatus1.ID=CrashInOut.InOutID ")
        sb.AppendLine(" left join CrashStatus CrashStatus2 on CrashStatus2.ID=CrashInOut.CrashStatusID ")
        sb.AppendLine(" LEFT JOIN WysInputUser ")
        sb.AppendLine(" ON CrashInOut.username=WysInputUser.id ")
        sb.AppendLine(" WHERE date(CrashInOut.DATETIME)>='" + strBegin + "'")
        sb.AppendLine("   AND date(CrashInOut.DATETIME)<='" + strEnd + "'")
        sb.AppendLine(" AND CrashInOut.TYPE = 2")

        Dim dtDataTable As DataTable = ct.FillTable(sb.ToString)
        Dim dv As DataView = dtDataTable.DefaultView
        dtDataTable = dv.ToTable

        Dim dtNew As New DataTable
        dtNew.TableName = "收入"
        dtNew.Columns.Add("编号")
        dtNew.Columns.Add("转出账户")
        dtNew.Columns.Add("转入账户")
        dtNew.Columns.Add("金额")
        dtNew.Columns.Add("备注")
        dtNew.Columns.Add("操作时间")
        dtNew.Columns.Add("操作用户")
        dtNew.Columns.Add("操作时间月")
        For Each dr As DataRow In dtDataTable.Rows
            dtNew.Rows.Add(New Object() {转换对象字符串(dr("ID")), 转换对象字符串(dr("Name1")), 转换对象字符串(dr("Name2")), 转换对象字符串(dr("CrashStatusCount")), _
                                                                    转换对象字符串(dr("MEMO")), 转换日期字符串(dr("DATETIME")), 转换对象字符串(dr("USERNAME1")), 转换日期字符串月(dr("DATETIME"))})
        Next
        Me.OutlookGrid1.AutoGenerateColumns = False
        Me.OutlookGrid1.BindData(dtNew, "收入")
        Me.OutlookGrid1.Columns(0).Visible = False

    End Sub

    Private Function 转换对象字符串(ByVal ob As Object) As String
        If ob Is DBNull.Value Then
            Return ""
        End If
        Return ob.ToString
    End Function

    Private Function 转换日期字符串(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月dd日")
    End Function

    Private Function 转换日期字符串月(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月")
    End Function
#End Region

#Region "删除CHECK"
    Public Overrides Function DoDeleteCheck() As Boolean
        DoDeleteCheck = False
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then
            ShowMessageBox("没有选中任何记录")
            Exit Function
        End If

        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Function
        Dim id As String = Me.OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString
        If id.Length <> 36 Then
            ShowMessageBox("没有选中任何记录")
            Exit Function
        End If
        strid = id
        If ShowMessageResult("是否删除本条记录") = Windows.Forms.DialogResult.No Then Exit Function

        DoDeleteCheck = True
    End Function
#End Region

#Region "重载删除事件"

    Private Sub UpdateCrashInOut()
        Dim sb As New StringBuilder
        Dim dt As New DataTable
        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * from CrashInOut  where id='" + strid + "'")
        dt = ct.FillTable(rs)
        If dt.Rows.Count > 0 Then
            Dim cmdSub As ADODB.Command = New ADODB.Command
            cmdSub.CommandType = ADODB.CommandTypeEnum.adCmdText
            For Each dr As DataRow In dt.Rows
                If CStr(dr("InOutID")) <> "" Then
                    sb.Length = 0
                    sb.AppendLine("Update CrashStatus set Count=Count +" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("InOutID")) + "'")
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("InOutID")))

                    '插入金额 2022-11-20
                    Account_Detail.Update转出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                    Crash_Detail.Update转出_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), CStr(dr("InOutID")), CDec(dr("CrashStatusCount")))

                End If

                If CStr(dr("CrashStatusID")) <> "" Then
                    sb.Length = 0
                    sb.AppendLine("Update CrashStatus set Count=Count -" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("CrashStatusID")) + "'")
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("CrashStatusID")))

                    '插入金额 2022-11-20
                    Account_Detail.Update转入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                    Crash_Detail.Update转入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), CStr(dr("CrashStatusID")), CDec(dr("CrashStatusCount")))
                End If
            Next
        End If

        'ct.GetTable("DELETE from CrashInOut  where id='" + strid + "' and type=2 ")
    End Sub

    Dim strid As String = ""
    Public Overrides Function DoDelete() As Boolean

        UpdateCrashInOut()

        Dim CMD As New ADODB.Command
        CMD.CommandText = "DELETE from CrashInOut  where ID='" + strid + "'"
        If ct.ExcCommand(CMD) Then
            ShowMessageBox("删除成功")
        Else
            ShowMessageBox("删除失败")
        End If

        LoadData1(0)
        OutUpdat_Click(Nothing, Nothing)
        Return True

    End Function

#End Region

#Region "重载插入"
    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Using frm As New AccountToAccount
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
            If frm.bAdd Then
                LoadData1(0)
                OutUpdat_Click(Nothing, Nothing)
            End If
        End Using
        Return True

    End Function
#End Region

    Private Sub MyDataView1_ItemDoubleClickRefreshData(ByVal strId As System.String, ByRef bState As System.Boolean) Handles MyDataView1.ItemDoubleClickRefreshData

    End Sub

#Region "重载设置窗体函数 A-2010-09-02"

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return False
    End Function
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadData1(0)
        OutUpdat_Click(Nothing, Nothing)
    End Sub

    Private Sub OutUpdat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.OutlookGrid1.RowCount = 0 Then Exit Sub
        Dim al As New List(Of DataGridViewColumn)
        Dim al1 As New List(Of ListSortDirection)

        If Me.chk转出账户.Checked Then
            al.Add(Me.OutlookGrid1.Columns("转出账户"))
            al1.Add(ListSortDirection.Ascending)
        End If

        If Me.chk转入账户.Checked Then
            al.Add(Me.OutlookGrid1.Columns("转入账户"))
            al1.Add(ListSortDirection.Descending)
        End If

        If Me.chk支出用户.Checked Then
            al.Add(Me.OutlookGrid1.Columns("操作用户"))
            al1.Add(ListSortDirection.Descending)
        End If

        If Me.chk支出时间.Checked Then
            al.Add(Me.OutlookGrid1.Columns("操作时间月"))
            al1.Add(ListSortDirection.Descending)
        End If
        If al.Count = 0 Then
            al.Add(Me.OutlookGrid1.Columns("转出账户"))
            al1.Add(ListSortDirection.Ascending)
        End If

        OutlookGrid1.GroupTemplate.Column = al.ToArray
        OutlookGrid1.Sort(al.ToArray, al1.ToArray)
        'If Me.rad全部折叠.Checked Then
        '    Me.OutlookGrid1.CollapseAll()
        'Else
        '    Me.OutlookGrid1.ExpandAll()
        'End If
    End Sub

    Private Sub OutlookGrid1_SGT(ByRef content As System.String, ByVal olg As Wys.Control.CSharp.OutlookGridRow, ByVal RowIndex As System.Collections.Generic.List(Of Integer)) Handles OutlookGrid1.SGT
        If Not olg.IsLastRow Then
            Dim 金额 As Decimal = 0
            For Each ri As Integer In RowIndex
                金额 += CDec(Me.OutlookGrid1.Rows(ri).Cells("金额").Value)
            Next
            content = olg.Group.Value.ToString.Trim & New String(" ", (60 - 得到GB2312编码.GetByteCount(olg.Group.Value.ToString.Trim))) & "  合计：" & 金额.ToString
        Else
            Dim 总金额 As Decimal = 0
            For Each dr As DataGridViewRow In Me.OutlookGrid1.Rows
                总金额 += CDec(dr.Cells("金额").Value)
            Next
            content = New String(" ", 60) & " 总合计：" & 总金额.ToString
        End If
    End Sub

    Private Sub rad全部折叠_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad全部折叠.Click, rad全部展开.Click
        If Me.rad全部折叠.Checked Then
            Me.OutlookGrid1.CollapseAll()
        Else
            Me.OutlookGrid1.ExpandAll()
        End If
    End Sub

    Private Sub OutlookGrid1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutlookGrid1.DoubleClick
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Sub
        Dim id As String = OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString
        If id.Length = 36 Then
            Using frm As AccountToAccount = New AccountToAccount
                frm.参数列表.Add("ID", id)
                frm.模式 = Wys.Control.Vb.InsertFormBase.模式型.修正
                frm.iType = Me.iType
                frm.strName = Me.strName
                frm.ShowDialog()
                If frm.bAdd Then
                    LoadData1(0)
                    OutUpdat_Click(Nothing, Nothing)
                End If
            End Using
        End If
    End Sub

    Public Overrides Sub 保存窗体数据到数据库(ByRef dtData As System.Data.DataTable)
        '年
        dtData.Rows.Add(New String() {"开始时间", "", dt开始时间.Value.ToString("yyyy-MM-dd")})
        dtData.Rows.Add(New String() {"结束时间", "", dt结束时间.Value.ToString("yyyy-MM-dd")})
        If chk转出账户.Checked Then
            dtData.Rows.Add(New String() {"转出账户", "", "是"})
        Else
            dtData.Rows.Add(New String() {"转出账户", "", "否"})
        End If

        If chk转入账户.Checked Then
            dtData.Rows.Add(New String() {"转入账户", "", "是"})
        Else
            dtData.Rows.Add(New String() {"转入账户", "", "否"})
        End If

        If chk支出用户.Checked Then
            dtData.Rows.Add(New String() {"支出用户", "", "是"})
        Else
            dtData.Rows.Add(New String() {"支出用户", "", "否"})
        End If

        If chk支出时间.Checked Then
            dtData.Rows.Add(New String() {"支出时间", "", "是"})
        Else
            dtData.Rows.Add(New String() {"支出时间", "", "否"})
        End If

        If Me.rad全部展开.Checked Then
            dtData.Rows.Add(New String() {"全部展开", "", "是"})
        Else
            dtData.Rows.Add(New String() {"全部展开", "", "否"})
        End If
        MyBase.保存窗体数据到数据库(dtData)
    End Sub

    Public Overrides Sub 存取数据库数据到窗体(ByVal dtDbData As System.Data.DataTable)

        If dtDbData.Rows.Count > 0 Then
            '年
            dt开始时间.Value = 查找数据("开始时间", "", Now.ToString("yyyy-MM-dd"), dtDbData)
            dt结束时间.Value = 查找数据("结束时间", "", Now.ToString("yyyy-MM-dd"), dtDbData)
            chk转出账户.Checked = IIf(查找数据("转出账户", "", "否", dtDbData) = "否", False, True)
            chk转入账户.Checked = IIf(查找数据("转入账户", "", "否", dtDbData) = "否", False, True)
            chk支出用户.Checked = IIf(查找数据("支出用户", "", "否", dtDbData) = "否", False, True)
            chk支出时间.Checked = IIf(查找数据("支出时间", "", "否", dtDbData) = "否", False, True)

            rad全部展开.Checked = IIf(查找数据("全部展开", "", "是", dtDbData) = "是", True, False)
            Me.rad全部折叠.Checked = IIf(查找数据("全部展开", "", "否", dtDbData) = "否", True, False)
        End If
        MyBase.存取数据库数据到窗体(dtDbData)
    End Sub

    Private Sub OutlookGrid1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OutlookGrid1.CellContentClick

    End Sub
End Class
